from django.db import models
from django.contrib.auth.models import User
from meetings.models import Group


class Event(models.Model):
    """活动信息"""
    STATUS_CHOICES = [
        ('upcoming', '即将开始'),
        ('ongoing', '进行中'),
        ('finished', '已结束'),
    ]
    
    title = models.CharField(max_length=200, verbose_name='活动标题', db_comment='活动标题')
    description = models.TextField(verbose_name='活动描述', db_comment='活动描述')
    cover_image = models.ImageField(upload_to='event_covers/', null=True, blank=True, verbose_name='封面图', db_comment='封面图路径')
    category = models.CharField(max_length=50, verbose_name='分类', db_comment='活动分类')
    tags = models.CharField(max_length=200, null=True, blank=True, verbose_name='标签', db_comment='标签,逗号分隔')
    organizer = models.CharField(max_length=100, null=True, blank=True, verbose_name='主办方', db_comment='主办方')
    start_date = models.DateField(verbose_name='开始日期', db_comment='开始日期')
    end_date = models.DateField(verbose_name='结束日期', db_comment='结束日期')
    registration_deadline = models.DateField(verbose_name='报名截止', db_comment='报名截止日期')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='upcoming', verbose_name='状态', db_comment='状态:upcoming即将开始,ongoing进行中,finished已结束')
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='created_events', verbose_name='创建者', db_comment='创建者ID')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间', db_comment='更新时间')

    class Meta:
        verbose_name = '活动'
        verbose_name_plural = '活动'
        db_table = 'zhahuo_event'
        db_table_comment = '活动信息表'
        ordering = ['-start_date']

    def __str__(self):
        return self.title


class EventRegistration(models.Model):
    """活动报名"""
    REGISTRATION_TYPE_CHOICES = [
        ('individual', '个人'),
        ('group', '小组'),
    ]
    
    STATUS_CHOICES = [
        ('pending', '待审核'),
        ('approved', '已通过'),
        ('rejected', '已驳回'),
    ]
    
    event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name='registrations', verbose_name='活动', db_comment='活动ID')
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='event_registrations', verbose_name='报名者', db_comment='报名者ID')
    group = models.ForeignKey(Group, on_delete=models.CASCADE, null=True, blank=True, related_name='event_registrations', verbose_name='报名小组', db_comment='报名小组ID')
    registration_type = models.CharField(max_length=20, choices=REGISTRATION_TYPE_CHOICES, verbose_name='类型', db_comment='类型:individual个人,group小组')
    materials = models.TextField(null=True, blank=True, verbose_name='报名材料', db_comment='报名材料')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='状态', db_comment='状态:pending待审核,approved已通过,rejected已驳回')
    registered_at = models.DateTimeField(auto_now_add=True, verbose_name='报名时间', db_comment='报名时间')

    class Meta:
        verbose_name = '活动报名'
        verbose_name_plural = '活动报名'
        db_table = 'zhahuo_event_registration'
        db_table_comment = '活动报名表'

    def __str__(self):
        return f'{self.event.title} - {self.registration_type}'


class EventPushLog(models.Model):
    """推送日志"""
    PUSH_METHOD_CHOICES = [
        ('notification', '通知'),
        ('email', '邮件'),
    ]
    
    event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name='push_logs', verbose_name='活动', db_comment='活动ID')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='event_pushes', verbose_name='推送用户', db_comment='推送用户ID')
    push_time = models.DateTimeField(auto_now_add=True, verbose_name='推送时间', db_comment='推送时间')
    push_method = models.CharField(max_length=20, choices=PUSH_METHOD_CHOICES, verbose_name='推送方式', db_comment='推送方式:notification通知,email邮件')
    is_read = models.BooleanField(default=False, verbose_name='是否已读', db_comment='是否已读')

    class Meta:
        verbose_name = '推送日志'
        verbose_name_plural = '推送日志'
        db_table = 'zhahuo_event_push_log'
        db_table_comment = '活动推送日志表'

    def __str__(self):
        return f'{self.event.title} -> {self.user.username}'
